<template>
	<view class="update-phone">
		<view class="my-img">
			<image src="@/static/images/profile/setting/phone.png" mode="" class="my-phone"></image>
		</view>
		<view class="title">
			当前绑定手机号码
		</view>
		<view class="title">
			+86 {{this.$store.state.user.user.phone+'' | hidePhone}}
		</view>
		<view class="form-group">
			<view class="phone-div">
				<u--input placeholder="请输入新的手机号码" prefixIcon="account" v-model="newPhone"
					prefixIconStyle="font-size: 22px;color: #909399">
				</u--input>
			</view>
			<view class="phone-div">
				<!-- #ifndef APP-NVUE -->
				<u-input placeholder="请输入验证码" prefixIcon="lock" v-model="code"
					prefixIconStyle="font-size: 22px;color: #909399">
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<u--input placeholder="请输入验证码" prefixIcon="lock" v-model="code"
						prefixIconStyle="font-size: 22px;color: #909399">
						<!-- #endif -->
						<template slot="suffix">
							<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
							<u-button @tap="getCode" :text="tips" type="primary" size="mini"></u-button>
						</template>
						<!-- #ifndef APP-NVUE -->
				</u-input>
				<!-- #endif -->
				<!-- #ifdef APP-NVUE -->
				</u--input>
				<!-- #endif -->
			</view>
			<view class="phone-div">
				<u-button type="primary" text="确认" @click="confirm"></u-button>
			</view>
			<view class="phone-div">
				<text class="tips">更换手机号码后，下次登录可使用新手机号码登录</text>
			</view>
		</view>
		<u-notify ref="uNotify"></u-notify>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		upd_phone,
		getUserInfo
	} from '@/api/contents/user/user.js'
	export default {
		data() {
			return {
				tips: '',
				newPhone: '',
				code: '',
			};
		},
		filters: {
			hidePhone(phone) {
				return phone.substr(0, 3) + "****" + phone.substring(7, 11);
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						this.showCode()
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			showCode() {
				setTimeout(() => {
					this.$refs.uNotify.show({
						type:'primary',
						message: '【小陈买车】您的验证码为:442211,一分钟内有效',
						duration: 1000 * 5,
						safeAreaInsetTop: true
					})
				}, 3000)
			},
			confirm() {
				upd_phone({
					newPhone:this.newPhone,
					code:this.code
				}).then(res=>{
					if(res.code==1){
						getUserInfo().then(res=>{
							this.$store.commit('updateUserInfo', res.data[0]);
						})
						this.$refs.uToast.show({
							message: res.msg,
							complete() {
								uni.navigateBack();
							}
						})
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg
						})
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.update-phone {
		padding: 0 100upx;

		.my-img {
			height: 250upx;
			display: flex;
			justify-content: center;
			align-items: flex-end;

			.my-phone {
				width: 68upx;
				height: 120upx;
			}
		}

		.title {
			height: 100upx;
			line-height: 100upx;
			text-align: center;
			font-size: 32upx;
			font-weight: 500;
			color: #414141;
			letter-spacing: 0.16px;
		}

		.phone-div {
			padding-bottom: 20upx;
			text-align: center;

			.tips {
				font-size: 20upx;
				font-weight: 400;
				color: #547bff;
				letter-spacing: 0.1px;
			}
		}
	}
</style>
